-------------------------------------------------------------------------------
--  The Legend of Zelda : Oracle of Secrets
--  Copyright (C) 2010 Pixelua
--
--  This program is free software: you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation, either version 3 of the License, or
--  (at your option) any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------------

weapons = {}

weapons.sword = {}
weapons.sword.icon = Image.load("img/Icons/swordL1.png", VRAM)
weapons.sword.key = "B"
weapons.sword.type = "ATK"
weapons.sword.had = true
weapons.sword.line = 0
weapons.sword.col = 0
weapons.sword.tmr = Timer.new()

weapons.sword.action = function()
	if not link.atk then
		link.spr:resetAnimation(link.direction+4)
		link.spr:startAnimation(link.direction+4)
		
		if link.direction == 1 then
			if math.ceil(link.casey) + 1 < 11 then
				if front_tile(link) == 7 or front_tile(link) == 8 then
					ScrollMap.setTile(actual_map_tab.file, math.ceil(link.casex), math.ceil(link.casey) + 1, 2)
					misc.grassDestr = true
					misc.grassSpr:startAnimation(1)
					misc.grassDestrX = link.casex
					misc.grassDestrY = link.casey + 1
				end
			end
		elseif link.direction == 4 then
			if math.ceil(link.casey) - 1 > 0 then
				if front_tile(link) == 7 or front_tile(link) == 8 then
					ScrollMap.setTile(actual_map_tab.file, math.ceil(link.casex), math.ceil(link.casey) - 1, 2)
					misc.grassDestr = true
					misc.grassSpr:startAnimation(1)
					misc.grassDestrX = link.casex
					misc.grassDestrY = link.casey - 1
				end
			end
		elseif link.direction == 3 then
			if math.ceil(link.casex) - 1 > 0 then
				if front_tile(link) == 7 or front_tile(link) == 8 then
					ScrollMap.setTile(actual_map_tab.file, math.ceil(link.casex) - 1, math.ceil(link.casey), 2)
					misc.grassDestr = true
					misc.grassSpr:startAnimation(1)
					misc.grassDestrX = link.casex - 1
					misc.grassDestrY = link.casey
				end
			end
		elseif link.direction == 2 then
			if math.ceil(link.casex) + 1 < 15 then
				if front_tile(link) == 7 or front_tile(link) == 8 then
					ScrollMap.setTile(actual_map_tab.file, math.ceil(link.casex) + 1, math.ceil(link.casey), 2)
					misc.grassDestr = true
					misc.grassSpr:startAnimation(1)
					misc.grassDestrX = link.casex + 1
					misc.grassDestrY = link.casey
				end
			end
		end
		link.atk = true
	elseif link.atk then
		weapons.sword.tmr:start()
		
		--Mls.gui:writeToConsole(weapons.sword.tmr:time())
		
		-- while weapons.sword.tmr:time() >= 1500 do
			-- link.spr:resetAnimation(link.direction+8)
			-- link.spr:startAnimation(link.direction+8)
			-- if Keys.released[weapons.sword.key] then
				-- weapons.sword.tmr:stop()
				-- weapons.sword.tmr:reset()
				-- break
			-- end
		-- end
	end
end

weapons.feather = {}
weapons.feather.icon = Image.load("img/Icons/featherL1.png", VRAM)
weapons.feather.key = ""
weapons.feather.type = "FLY"
weapons.feather.had = false
weapons.feather.line = 0
weapons.feather.col = 0

weapons.feather.action = function()
	
end

weapons.strengh = {}
weapons.strengh.icon = Image.load("img/Icons/strenghL1.png", VRAM)
weapons.strengh.key = ""
weapons.strengh.type = "STR"
weapons.strengh.lv = "L1"
weapons.strengh.had = false
weapons.strengh.line = 0
weapons.strengh.col = 0

weapons.strengh.action = function()
	
end

weapons_nb = 1

weapons_position = {}
weapons_position["B"] = 6
weapons_position["A"] = 46
weapons_position["X"] = 86
weapons_position["Y"] = 126

weapons_keys = {}
weapons_keys["B"] = weapons.sword
weapons_keys["A"] = ""
weapons_keys["X"] = ""
weapons_keys["Y"] = ""

weapons_update = function()
	for k, v in pairs(weapons) do
		if weapons[k].key ~= "" and weapons[k].had then
			screen.blit(SCREEN_UP, weapons_position[weapons[k].key], 175, weapons[k].icon)
			if Keys.held[weapons[k].key] and not misc.paused then
				weapons[k].action()
			end
		end
	end
end
